/*
 * @Author: 0x9DEFA478
 * @Date: 2021-10-23 23:45:32
 * @LastEditTime: 2022-03-06 20:20:28
 * @LastEditors: 0x9DEFA478
 * @Description: 
 * QQ:2652450237
 * ============================================================================================================================================
 * 
 * 
 * 
 *                                                                                               ************      ****************************
 *                                                                                             ************      ****************************  
 *                                                                                           ************      ****************************    
 *                                                                                         ************      ****************************      
 *                                                                                       ************      ************                        
 *                                                                                     ************      ************                          
 *                                                                                   ************      ************                            
 *                                                                                 ************      ************                              
 *                                                                               ************      ************                                
 *                                                                             ************      ************                                  
 *                                                                           ************      ************                                    
 *                                                                         ************      ************                                      
 *                                                                       ************      ************                                        
 *                                                                     ************      ************                                          
 *                                                                   ************      ************                                            
 *                                                                 ************      ************                                              
 *                                                               ************      ************                                                
 *                                                             ************      ************                                                  
 *                                                           ************      ************                                                    
 *                                                         ************      ************                                                      
 *                                                       ************      ************                                                        
 *                                                     ************      ************                                                          
 *                                                   ************      ************                                                            
 *                                                 ************      ************                                                              
 *                                               ************      ************                                                                
 *                                             ************      ************                                                                  
 *                                           ************      ************                                                                    
 *                                         ************      ************                                                                      
 *                                       ************      ************                                                                        
 *                                     ************      ************                                                                          
 *                                   ************      ************                                                                            
 *                                 ************      ************                                                                              
 *                               ************      ************                                                                                
 *                             ************      ************                                                                                  
 *                           ************      ************                                                                                    
 *                         ************      ************                                                                                      
 *       ****************************      ************                                                                                        
 *     ****************************      ************                                                                                          
 *   ****************************      ************                                                                                            
 * ****************************      ************                                                                                              
 * 
 * 
 * 
 * ============================================================================================================================================
 * 
 */
#include "app_main.h"
#include "System/System.h"
#include "H_String.h"
#include "H_ThreadScheduler.h"
#include "MemoryMalloc.h"
#include "I_DebugPort.h"
#include "H_Printf.h"
#include "Peripherals_Instence.h"
#include "IP_GPIO.h"
#include "Peripheral.h"

H_TS_Thread* app_main_ThreadHandle;



volatile static float CPU_Temperatrue=0.0f;

int CPU_Temp_Thread(){

  float Temperatrue;
  int data[2];

  Temperatrue=0.0f;
  for(;;){
    Temperatrue+=(IP_ADC_ValueToTemperatrue(IP_ADC_GetValue(ip_adc3),0x10000U)-Temperatrue)*0.04f;
    CPU_Temperatrue=Temperatrue;
    H_TS_ThreadSleep(10);

  }
}


static int IsCMD(Hbyte* Buffer,int BufferLength,char* CMD){

  while(BufferLength>0){
    BufferLength--;

    if(CMD[0]=='\0'){
      return 0;
    }

    if(CMD[0]!=Buffer[0]){
      return 0;
    }

    CMD=&CMD[1];
    Buffer=&Buffer[1];
  }

  return -1;
}

#include "nes.h"

extern volatile Huint16 StandardControllerStatus;

//捕捉一个指令 并作出响应
void Debug_Execute(void* Buffer,int MaxLength){
  int length;
  int i;
  int Time;
  Hbyte* str;

  H_Malloc_Info info;
  H_TS_ThreadInfo* thread_info;
  H_TS_ThreadInfo* thread_info_item;

  str=Buffer;

  
  length=I_DebugPort_Read(Buffer,1);
  if(length<=0){
    return;
  }

  if(str[0]==' '){//指令标识符

    length=I_DebugPort_Read(Buffer,MaxLength);
    if(length<=0){
      return;
    }

    if(str[0]=='J'){
      Huint16 t;

      if(length<9){
        return;
      }

      t=StandardControllerStatus;

      str=&str[1];

      for(i=0;i<8;i++){
        if(str[i]=='P'){
          t|=0x0001U<<i;
        }else{
          t&=(~(0x0001U<<i));
        }
      }

      StandardControllerStatus=t;
      return;
    }else if(str[0]=='F'){
      if(length<2){
        return;
      }

      nes_cmd(str[1]);

      return;
    }


    if(0!=IsCMD(str,length,"GetStackInfo")){

      MemoryMalloc_DTCM.GetInfo(&info);
      H_Printf("DTCM:{f.2}%\r\n  Result:{i}\r\n  可使用:{i}Byte\r\n  总共:{i}Byte\r\n",100.0f-(float)info.FreeSize*100/(info.NoOccupySize+info.OccupySize)
        ,info.Result,info.FreeSize,info.NoOccupySize+info.OccupySize);
      MemoryMalloc_AXISRAM.GetInfo(&info);
      H_Printf("AXI:{f.2}%\r\n  Result:{i}\r\n  可使用:{i}Byte\r\n  总共:{i}Byte\r\n",100.0f-(float)info.FreeSize*100/(info.NoOccupySize+info.OccupySize)
        ,info.Result,info.FreeSize,info.NoOccupySize+info.OccupySize);
      MemoryMalloc_D2SRAM.GetInfo(&info);
      H_Printf("D2:{f.2}%\r\n  Result:{i}\r\n  可使用:{i}Byte\r\n  总共:{i}Byte\r\n",100.0f-(float)info.FreeSize*100/(info.NoOccupySize+info.OccupySize)
        ,info.Result,info.FreeSize,info.NoOccupySize+info.OccupySize);
      MemoryMalloc_D3SRAM.GetInfo(&info);
      H_Printf("D3:{f.2}%\r\n  Result:{i}\r\n  可使用:{i}Byte\r\n  总共:{i}Byte\r\n",100.0f-(float)info.FreeSize*100/(info.NoOccupySize+info.OccupySize)
        ,info.Result,info.FreeSize,info.NoOccupySize+info.OccupySize);
      
      H_Printf("RTOS堆栈占用率:{f.2}%\r\n",(float)H_TS_GetStackUtilization()/10.0f);
      

      H_Printf("线程堆栈:{i}Byte\r\n",H_TS_GetThisThread()->StackMaxUsage);


      H_Printf("CPU占用率:{f.2}%\r\nCPU温度:{f.2}℃\r\n",(float)H_TS_GetCPU_Load()/10.0f,CPU_Temperatrue);

    }else if(0!=IsCMD(str,length,"GetThreadInfo")){

      thread_info=H_TS_ThreadInfoGet();

      thread_info_item=thread_info;
      while(thread_info_item!=NULL){
         
        str=thread_info_item->ThreadName;
        if(str==NULL){
          str="<未命名>";
        }

        H_Printf("线程信息:\r\n  线程名:{s}\r\n  CPU占用率:{f.2}%\r\n  优先级:{i}\r\n  堆栈占用:{i}/{i}={f.2}%\r\n\r\n",str,(float)thread_info_item->CPU_Load/10.0f,thread_info_item->Priority,thread_info_item->StackMaxUsage,thread_info_item->StackSize,100.0*(float)thread_info_item->StackMaxUsage/thread_info_item->StackSize);
        
        thread_info_item=thread_info_item->Next;
      }

      H_TS_ThreadInfoFree(thread_info);


    }else if(0!=IsCMD(str,length,"Block")){
      H_Printf("阻塞一段时间...\r\n");
      HAL_Delay(1024);
      H_Printf("阻塞停止\r\n");
    }else{
      H_Printf("无效指令\r\n");
    }


  }

  
}




unsigned char NESPalette_FCEUX[]={
  0x74,0x74,0x74,0x24,0x18,0x8C,0x00,0x00,0xA8,0x44,0x00,0x9C,0x8C,0x00,0x74,0xA8,0x00,0x10,0xA4,0x00,0x00,0x7C,0x08,0x00,0x40,0x2C,0x00,0x00,0x44,0x00,0x00,0x50,0x00,0x00,0x3C,0x14,0x18,0x3C,0x5C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0xBC,0xBC,0xBC,0x00,0x70,0xEC,0x20,0x38,0xEC,0x80,0x00,0xF0,0xBC,0x00,0xBC,0xE4,0x00,0x58,0xD8,0x28,0x00,0xC8,0x4C,0x0C,0x88,0x70,0x00,0x00,0x94,0x00,0x00,0xA8,0x00,0x00,0x90,0x38,0x00,0x80,0x88,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0xFC,0xFC,0xFC,0x3C,0xBC,0xFC,0x5C,0x94,0xFC,0xCC,0x88,0xFC,0xF4,0x78,0xFC,0xFC,0x74,0xB4,0xFC,0x74,0x60,0xFC,0x98,0x38,0xF0,0xBC,0x3C,0x80,0xD0,0x10,0x4C,0xDC,0x48,0x58,0xF8,0x98,0x00,0xE8,0xD8,0x78,0x78,0x78,0x00,0x00,0x00,0x00,0x00,0x00,
  0xFC,0xFC,0xFC,0xA8,0xE4,0xFC,0xC4,0xD4,0xFC,0xD4,0xC8,0xFC,0xFC,0xC4,0xFC,0xFC,0xC4,0xD8,0xFC,0xBC,0xB0,0xFC,0xD8,0xA8,0xFC,0xE4,0xA0,0xE0,0xFC,0xA0,0xA8,0xF0,0xBC,0xB0,0xFC,0xCC,0x9C,0xFC,0xF0,0xC4,0xC4,0xC4,0x00,0x00,0x00,0x00,0x00,0x00
};

unsigned char NESPalette_Default[]={
  0x75,0x75,0x75,0x27,0x1B,0x8F,0x00,0x00,0xAB,0x47,0x00,0x9F,0x8F,0x00,0x77,0xAB,0x00,0x13,0xA7,0x00,0x00,0x7F,0x0B,0x00,0x43,0x2F,0x00,0x00,0x47,0x00,0x00,0x51,0x00,0x00,0x3F,0x17,0x1B,0x3F,0x5F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0xBC,0xBC,0xBC,0x00,0x73,0xEF,0x23,0x3B,0xEF,0x83,0x00,0xF3,0xBF,0x00,0xBF,0xE7,0x00,0x5B,0xDB,0x2B,0x00,0xCB,0x4F,0x0F,0x8B,0x73,0x00,0x00,0x97,0x00,0x00,0xAB,0x00,0x00,0x93,0x3B,0x00,0x83,0x8B,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0xFF,0xFF,0xFF,0x3F,0xBF,0xFF,0x5F,0x97,0xFF,0xA7,0x8B,0xFD,0xF7,0x7B,0xFF,0xFF,0x77,0xB7,0xFF,0x77,0x63,0xFF,0x9B,0x3B,0xF3,0xBF,0x3F,0x83,0xD3,0x13,0x4F,0xDF,0x4B,0x58,0xF8,0x98,0x00,0xEB,0xDB,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0xFF,0xFF,0xFF,0xAB,0xE7,0xFF,0xC7,0xD7,0xFF,0xD7,0xCB,0xFF,0xFF,0xC7,0xFF,0xFF,0xC7,0xDB,0xFF,0xBF,0xB3,0xFF,0xDB,0xAB,0xFF,0xE7,0xA3,0xE3,0xFF,0xA3,0xAB,0xF3,0xBF,0xB3,0xFF,0xCF,0x9F,0xFF,0xF3,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
};

unsigned char NESPalette_2C02[]={
  0x54,0x54,0x54,0x00,0x1E,0x74,0x08,0x10,0x90,0x30,0x00,0x88,0x44,0x00,0x64,0x5C,0x00,0x30,0x54,0x04,0x00,0x3C,0x18,0x00,0x20,0x2A,0x00,0x08,0x3A,0x00,0x00,0x40,0x00,0x00,0x3C,0x00,0x00,0x32,0x3C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x98,0x96,0x98,0x08,0x4C,0xC4,0x30,0x32,0xEC,0x5C,0x1E,0xE4,0x88,0x14,0xB0,0xA0,0x14,0x64,0x98,0x22,0x20,0x78,0x3C,0x00,0x54,0x5A,0x00,0x28,0x72,0x00,0x08,0x7C,0x00,0x00,0x76,0x28,0x00,0x66,0x78,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0xEC,0xEE,0xEC,0x4C,0x9A,0xEC,0x78,0x7C,0xEC,0xB0,0x62,0xEC,0xE4,0x54,0xEC,0xEC,0x58,0xB4,0xEC,0x6A,0x64,0xD4,0x88,0x20,0xA0,0xAA,0x00,0x74,0xC4,0x00,0x4C,0xD0,0x20,0x38,0xCC,0x6C,0x38,0xB4,0xCC,0x3C,0x3C,0x3C,0x00,0x00,0x00,0x00,0x00,0x00,
  0xEC,0xEE,0xEC,0xA8,0xCC,0xEC,0xBC,0xBC,0xEC,0xD4,0xB2,0xEC,0xEC,0xAE,0xEC,0xEC,0xAE,0xD4,0xEC,0xB4,0xB0,0xE4,0xC4,0x90,0xCC,0xD2,0x78,0xB4,0xDE,0x78,0xA8,0xE2,0x90,0x98,0xE2,0xB4,0xA0,0xD6,0xE4,0xA0,0xA2,0xA0,0x00,0x00,0x00,0x00,0x00,0x00
};

unsigned char NESPalette_2C03_2C05[]={
  0x60,0x60,0x60,0x00,0x20,0x80,0x00,0x00,0xC0,0x60,0x40,0xC0,0x80,0x00,0x60,0xA0,0x00,0x60,0xA0,0x20,0x00,0x80,0x40,0x00,0x60,0x40,0x00,0x20,0x40,0x00,0x00,0x60,0x20,0x00,0x80,0x00,0x00,0x40,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0xA0,0xA0,0xA0,0x00,0x60,0xC0,0x00,0x40,0xE0,0x80,0x00,0xE0,0xA0,0x00,0xE0,0xE0,0x00,0x80,0xE0,0x00,0x00,0xC0,0x60,0x00,0x80,0x60,0x00,0x20,0x80,0x00,0x00,0x80,0x00,0x00,0xA0,0x60,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0xE0,0xE0,0xE0,0x60,0xA0,0xE0,0x80,0x80,0xE0,0xC0,0x60,0xE0,0xE0,0x00,0xE0,0xE0,0x60,0xE0,0xE0,0x80,0x00,0xE0,0xA0,0x00,0xC0,0xC0,0x00,0x60,0xC0,0x00,0x00,0xE0,0x00,0x40,0xE0,0xC0,0x00,0xE0,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0xE0,0xE0,0xE0,0xA0,0xC0,0xE0,0xC0,0xA0,0xE0,0xE0,0xA0,0xE0,0xE0,0x80,0xE0,0xE0,0xA0,0xA0,0xE0,0xC0,0x80,0xE0,0xE0,0x40,0xE0,0xE0,0x60,0xA0,0xE0,0x40,0x80,0xE0,0x60,0x40,0xE0,0xC0,0x80,0xC0,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
};

unsigned char NESPalette_RC2C03B[]={
  0x60,0x60,0x60,0x00,0x20,0x80,0x00,0x00,0xC0,0x60,0x40,0xC0,0x80,0x00,0x60,0xA0,0x00,0x60,0xA0,0x20,0x00,0x80,0x40,0x00,0x60,0x40,0x00,0x20,0x00,0x00,0x00,0x60,0x20,0x00,0x80,0x00,0x00,0x40,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0xA0,0xA0,0xA0,0x00,0x20,0xC0,0x00,0x40,0xE0,0x80,0x00,0xE0,0xA0,0x00,0xE0,0xE0,0x00,0x80,0xE0,0x00,0x00,0xC0,0x60,0x00,0x80,0x60,0x00,0x20,0x80,0x00,0x00,0x80,0x00,0x00,0xA0,0x60,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0xE0,0xE0,0xE0,0x60,0xA0,0xE0,0x80,0x80,0xE0,0xC0,0x60,0xE0,0xE0,0x00,0xE0,0xE0,0x20,0xE0,0xE0,0x80,0x00,0xE0,0xA0,0x00,0xC0,0xC0,0x00,0x60,0x80,0x00,0x00,0xE0,0x00,0x40,0xE0,0xC0,0x00,0xE0,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0xE0,0xE0,0xE0,0xA0,0x80,0xE0,0xC0,0xA0,0xE0,0xE0,0xA0,0xE0,0xE0,0x80,0xE0,0xE0,0xA0,0xA0,0xE0,0xC0,0x80,0xE0,0xE0,0x40,0xE0,0xE0,0x60,0xA0,0xA0,0x40,0x80,0xE0,0x60,0x40,0xE0,0xC0,0x80,0xC0,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
};

unsigned char NESPalette_2C04[]={
  0x60,0x60,0x60,0x00,0x20,0x80,0x00,0x00,0xC0,0x60,0x40,0xC0,0x80,0x00,0x60,0xA0,0x00,0x60,0xA0,0x20,0x00,0x80,0x40,0x00,0x60,0x40,0x00,0x20,0x40,0x00,0x00,0x60,0x20,0x00,0x00,0x00,0x00,0x40,0x40,0x20,0x20,0x20,0x00,0x00,0x60,0x00,0x40,0x00,
  0xA0,0xA0,0xA0,0x00,0x60,0xC0,0x00,0x40,0xE0,0x80,0x00,0xE0,0xA0,0x00,0xE0,0xE0,0x00,0x80,0xE0,0x00,0x00,0xC0,0x60,0x00,0x80,0x60,0x00,0x20,0x80,0x00,0x00,0x80,0x00,0x00,0xA0,0x60,0x00,0x80,0x80,0x40,0x40,0x40,0x40,0x00,0x00,0x60,0x20,0x00,
  0xE0,0xE0,0xE0,0x60,0xA0,0xE0,0x80,0x80,0xE0,0xC0,0x60,0xE0,0xE0,0x00,0xE0,0xE0,0x60,0xE0,0xE0,0x80,0x00,0xE0,0xA0,0x00,0xC0,0xC0,0x00,0x60,0xC0,0x00,0x00,0xE0,0x00,0x00,0x00,0x00,0x00,0xE0,0xE0,0x80,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,
  0xE0,0xE0,0xE0,0xA0,0xC0,0xE0,0xC0,0xA0,0xE0,0xE0,0xA0,0xE0,0xE0,0x80,0xE0,0xE0,0xA0,0xA0,0xE0,0xC0,0x80,0xE0,0xE0,0x40,0xE0,0xE0,0x60,0xA0,0xE0,0x40,0x80,0xE0,0x60,0x40,0xE0,0xC0,0x80,0xC0,0xE0,0xC0,0xC0,0xC0,0xC0,0xA0,0x60,0xE0,0xC0,0x00
};

unsigned char NESPalette_RP2C04_0001[]={
  0xE0,0xA0,0xA0,0xC0,0x60,0xE0,0xE0,0x00,0x00,0x80,0x80,0xE0,0x00,0x80,0x80,0x20,0x40,0x00,0x40,0x40,0x40,0xE0,0x00,0x80,0xE0,0xE0,0xE0,0x60,0x60,0x60,0xE0,0xA0,0x00,0xA0,0x00,0x60,0x80,0x00,0x60,0xC0,0xC0,0x00,0x60,0x40,0x00,0xE0,0xE0,0xE0,
  0x60,0xA0,0xE0,0xC0,0xA0,0x60,0x60,0x20,0x00,0x60,0xC0,0x00,0x80,0xC0,0xE0,0xC0,0xA0,0xE0,0xE0,0xC0,0x80,0x00,0x40,0xE0,0xE0,0xC0,0x00,0x40,0xE0,0xC0,0x00,0x00,0x00,0x40,0x00,0x00,0xC0,0xC0,0xC0,0x80,0x80,0x80,0xE0,0x00,0xE0,0x00,0x20,0x80,
  0x00,0x00,0x60,0xA0,0xC0,0xE0,0xE0,0xA0,0xE0,0x00,0xE0,0x00,0x00,0xE0,0xE0,0x00,0x40,0x40,0x00,0xA0,0x60,0xA0,0x00,0xE0,0x00,0x00,0x00,0x80,0x40,0x00,0xE0,0x80,0xE0,0xA0,0x20,0x00,0x80,0x00,0xE0,0x00,0x00,0xC0,0xE0,0x80,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x20,0x80,0x00,0xA0,0xA0,0xA0,0x00,0x60,0x20,0xA0,0xE0,0x40,0x60,0x40,0xC0,0xE0,0xE0,0x00,0xC0,0x60,0x00,0x00,0x40,0x00,0x00,0x60,0xC0,0x00,0x80,0x00,0x20,0x20,0x20,0xE0,0xE0,0x60,0xE0,0x60,0xE0,0x80,0x60,0x00,0x80,0xE0,0x60
};

unsigned char NESPalette_RP2C04_0002[]={
  0x00,0x00,0x00,0xE0,0xA0,0x00,0x80,0x60,0x00,0xA0,0xE0,0x40,0x80,0xE0,0x60,0xE0,0x60,0xE0,0x00,0x80,0x80,0xA0,0xC0,0xE0,0xE0,0x00,0x00,0x80,0x00,0xE0,0xE0,0xE0,0x60,0xE0,0x80,0xE0,0xE0,0xE0,0xE0,0xC0,0x60,0xE0,0x80,0xC0,0xE0,0x00,0x80,0x00,
  0x00,0x40,0x00,0x60,0xA0,0xE0,0xA0,0x20,0x00,0xC0,0xC0,0xC0,0x00,0xA0,0x60,0x60,0xC0,0x00,0x40,0x00,0x00,0x80,0x80,0xE0,0x40,0x40,0x40,0xE0,0x00,0xE0,0x00,0x00,0x60,0x40,0xE0,0xC0,0xC0,0xA0,0xE0,0x60,0x40,0x00,0x00,0x00,0x00,0x60,0x40,0xC0,
  0x80,0x00,0x60,0xE0,0xC0,0x80,0xE0,0x80,0x00,0xE0,0xA0,0xE0,0x00,0x60,0xC0,0x60,0x20,0x00,0xA0,0xA0,0xA0,0x00,0x00,0xC0,0xA0,0x00,0xE0,0xE0,0xC0,0x00,0x60,0x60,0x60,0x20,0x40,0x00,0x00,0x40,0xE0,0x00,0x00,0x00,0xC0,0xC0,0x00,0xE0,0xE0,0xE0,
  0xC0,0xA0,0x60,0x20,0x20,0x20,0x00,0xE0,0x00,0xC0,0x60,0x00,0x00,0x40,0x40,0x00,0x20,0x80,0xE0,0x00,0x80,0x20,0x80,0x00,0x00,0x00,0x00,0x00,0xE0,0xE0,0x80,0x40,0x00,0xE0,0xE0,0x00,0xE0,0xA0,0xA0,0xA0,0x00,0x60,0x00,0x60,0x20,0x80,0x80,0x80
};

unsigned char NESPalette_RP2C04_0003[]={
  0xA0,0x00,0xE0,0xE0,0x60,0xE0,0x80,0xE0,0x60,0xA0,0xA0,0xA0,0x00,0x80,0x00,0xE0,0xE0,0xE0,0xA0,0xC0,0xE0,0x20,0x40,0x00,0x00,0x20,0x80,0x00,0x00,0x00,0xE0,0xC0,0x80,0x60,0x40,0x00,0xE0,0x00,0x80,0xC0,0xC0,0xC0,0xC0,0xA0,0x60,0x80,0xC0,0xE0,
  0x80,0x80,0xE0,0x00,0x80,0x80,0xA0,0x00,0x60,0x00,0x40,0xE0,0x20,0x80,0x00,0x80,0x60,0x00,0xC0,0x60,0x00,0x00,0xA0,0x60,0x60,0x60,0x60,0x60,0x40,0xC0,0x00,0x00,0x00,0x00,0x00,0xC0,0xE0,0x00,0x00,0xA0,0x20,0x00,0xE0,0x80,0xE0,0xE0,0xA0,0xA0,
  0xC0,0x60,0xE0,0x00,0x40,0x00,0x00,0x00,0x60,0xE0,0xE0,0x00,0x20,0x20,0x20,0xE0,0xA0,0x00,0xE0,0x80,0x00,0xE0,0xE0,0xE0,0x60,0xC0,0x00,0x80,0x00,0x60,0x60,0xA0,0xE0,0xE0,0x00,0xE0,0x00,0x60,0xC0,0x80,0x80,0x80,0x00,0x00,0x00,0x60,0x20,0x00,
  0x00,0xE0,0xE0,0x40,0x00,0x00,0xA0,0xE0,0x40,0xE0,0xA0,0xE0,0x80,0x40,0x00,0x00,0xE0,0x00,0xC0,0xC0,0x00,0x40,0x40,0x40,0x00,0x60,0x20,0x00,0x00,0x00,0xC0,0xA0,0xE0,0xE0,0xE0,0x60,0x80,0x00,0xE0,0x40,0xE0,0xC0,0xE0,0xC0,0x00,0x00,0x40,0x40
};

unsigned char NESPalette_RP2C04_0004[]={
  0x80,0x60,0x00,0x60,0x40,0xC0,0x00,0x80,0x80,0xC0,0xC0,0x00,0x00,0x00,0x00,0xE0,0xA0,0xA0,0x00,0x20,0x80,0xC0,0x60,0x00,0xA0,0xA0,0xA0,0x60,0x20,0x00,0x00,0xE0,0x00,0x00,0x00,0x60,0xE0,0xC0,0x80,0xE0,0xE0,0x00,0x00,0x80,0x00,0xA0,0xE0,0x40,
  0xE0,0x60,0xE0,0x40,0x00,0x00,0x00,0x40,0xE0,0xE0,0x80,0xE0,0x00,0x00,0x00,0x40,0x40,0x40,0xA0,0x20,0x00,0xE0,0x80,0x00,0xC0,0xA0,0x60,0x00,0xA0,0x60,0x80,0x80,0xE0,0x20,0x80,0x00,0x80,0x00,0x60,0x00,0x00,0x00,0x80,0xE0,0x60,0x60,0xA0,0xE0,
  0xA0,0x00,0x60,0x00,0x60,0x20,0x80,0x40,0x00,0x00,0x00,0xC0,0x80,0x00,0xE0,0xA0,0x00,0xE0,0x60,0x60,0x60,0xE0,0x00,0x80,0x00,0x40,0x40,0xC0,0xC0,0xC0,0x00,0x60,0xC0,0x00,0x40,0x00,0x20,0x20,0x20,0xE0,0xE0,0x60,0x80,0x80,0x80,0xE0,0x00,0xE0,
  0xE0,0xA0,0xE0,0xE0,0xE0,0xE0,0x60,0x40,0x00,0xE0,0x00,0x00,0xE0,0xC0,0x00,0x40,0xE0,0xC0,0xE0,0xE0,0xE0,0x80,0xC0,0xE0,0x00,0x00,0x00,0xE0,0xA0,0x00,0xC0,0x60,0xE0,0xA0,0xC0,0xE0,0x60,0xC0,0x00,0xC0,0xA0,0xE0,0x00,0xE0,0xE0,0x20,0x40,0x00
};



//主线程函数, 不要在其他地方调用这个
int app_main(void* v){
  void* Buffer;
  int i;

  app_main_ThreadHandle=H_TS_GetThisThread();
  if(cH_IsNull(app_main_ThreadHandle)){
    for(;;){
    }
  }
  app_main_ThreadHandle->ThreadName="app_main";

  //SCB_CleanInvalidateDCache();

  RCC->AHB4ENR|=RCC_AHB4ENR_BKPRAMEN;
  PWR->CR1|=PWR_CR1_DBP;
  if((PWR->CR2&PWR_CR2_BRRDY)==0){
    PWR->CR2|=PWR_CR2_BREN;
  }
  while((PWR->CR2&PWR_CR2_BRRDY)==0){
  }
  System_DataMemoryBarrier();
  H_Printf("\r\n启动标记:0x{X8}\r\n",((int*)0x38800000U)[0]);
  ((int*)0x38800000U)[0]++;
  System_DataMemoryBarrier();
  SCB_CleanInvalidateDCache_by_Addr((void*)0x38800000U,4);

  H_Printf("\r\n启动标记(RAM):0x{X8}\r\n",((int*)0x3800F000U)[0]);
  ((int*)0x3800F000U)[0]++;


  H_TS_StartNewThread(CPU_Temp_Thread,NULL,1024,1,0)->ThreadName="CPU_Temp_Thread";
  
  nes_Init();

  Buffer=MemoryMalloc_AXISRAM.Malloc(2048);
  for(;;){
    
    Debug_Execute(Buffer,2048);

    

  }

  return 0;
}


